package com.badlogic.androidgames.game;

import java.util.LinkedList;

import android.graphics.Rect;

import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Pixmap;
import com.badlogic.androidgames.framework.Game;

public class Player extends ControlledAnimation {

	GameObject leftArrow, rightArrow, jumpButton;
	
	int[] index = new int[3];
	LinkedList<Integer> usedTouch = new LinkedList<Integer>();
	
	float oldPosX;
	float oldPosY;
	
	public Player(Pixmap texture, Vector2 p, Vector2 d, Vector2 sD) {
		super(texture, p, d, sD);
		
		leftArrow = new GameObject(Assets.LeftArrow, new Vector2(75, 225), new Vector2(50, 50));
		rightArrow = new GameObject(Assets.RightArrow, new Vector2(150, 225), new Vector2(50, 50));
		jumpButton = new GameObject(Assets.Jump, new Vector2(350, 225), new Vector2(50, 50));
		
	}
	
	enum STATE
	{
		PULANDO,
		NA_CORDA,
		NA_ESCADA,
		NO_CHAO,
		MORTO
	}
	
	public STATE statePlayer = STATE.NO_CHAO;
	private float vel = 90;
	private float jumpVel = 90;
	private float gravity = 180;
	
	
	public void Update(Game game, float deltaTime)
	{
		oldPosX = pos.x;
		oldPosY = pos.y;
		
		switch (this.statePlayer) 
		{
			case NA_CORDA:
				break;
				
			case NO_CHAO:
				if (tex == Assets.PlayerJumpD)
					SetTexture(Assets.PlayerWalkE, new Vector2(19, 22));
				if (tex == Assets.PlayerJumpE)
					SetTexture(Assets.PlayerWalkD, new Vector2(19, 22));
				MoveY(gravity * deltaTime);
				AdjustCollision();
				break;
				
			case NA_ESCADA:
				break;
				
			case PULANDO:
				MoveY(-jumpVel * deltaTime);
				jumpVel -= gravity * deltaTime;
				AdjustCollision();
				break;
				
			case MORTO:
				break;
				
		}
		
		UpdateInput(game, deltaTime);
	}
	
	public void UpdateInput(Game game, float deltaTime)
	{
		boolean updateAnimation = false;
		
		for (int i = 0; i < index.length; i++)
			index[i] = -1;
		
		usedTouch.clear();
		
		for (int i = 0; i < 10; i++)
		{
			//Seta Esquerda
			if (game.getInput().isTouchDown(i) && index[0] == i ||
				game.getInput().isTouchDown(i) && index[0] == -1 && !usedTouch.contains(i))
			{
				if (leftArrow.rectangle.contains(game.getInput().getTouchX(i),
											     game.getInput().getTouchY(i)))
				{
					index[0] = i;
					usedTouch.add(i);
					
					if (statePlayer == STATE.NO_CHAO)
						this.SetTexture(Assets.PlayerWalkD, new Vector2(19, 22));
					else if (statePlayer == STATE.PULANDO)
						this.SetTexture(Assets.PlayerJumpE, new Vector2(19, 21));
					
					MoveX(-vel * deltaTime);
					AdjustCollision();
					
					updateAnimation = true;
					
					super.Update(deltaTime);
				}
			}
			
			//Seta Direita
			if (game.getInput().isTouchDown(i) && index[1] == i ||
					game.getInput().isTouchDown(i) && index[1] == -1 && !usedTouch.contains(i))
				{
					if (rightArrow.rectangle.contains(game.getInput().getTouchX(i),
												     game.getInput().getTouchY(i)))
					{
						index[1] = i;
						usedTouch.add(i);
						
						if (statePlayer == STATE.NO_CHAO)
							this.SetTexture(Assets.PlayerWalkE, new Vector2(19, 22));
						else if (statePlayer == STATE.PULANDO)
							this.SetTexture(Assets.PlayerJumpD, new Vector2(19, 21));
						
						MoveX(vel * deltaTime);
						AdjustCollision();
						
						updateAnimation = true;
						
						super.Update(deltaTime);
						
					}
				}
			
			//Botao Pulo
			if (game.getInput().isTouchDown(i) && index[2] == i ||
					game.getInput().isTouchDown(i) && index[2] == -1 && !usedTouch.contains(i))
				{
					if (jumpButton.rectangle.contains(game.getInput().getTouchX(i),
												     game.getInput().getTouchY(i)))
					{
						if (statePlayer == STATE.NO_CHAO)
						{
							jumpVel = 90;
							statePlayer = STATE.PULANDO;
						}
					}
				}
		}
		
		if (!updateAnimation)
			this.GoFirstFrame();
	}
	
	public void AdjustCollision()
	{
		for (int i = 0; i < StaticObjects.walls.length; i++)
		{
			if (CollisionManager.CheckCollision(this, StaticObjects.walls[i], oldPosX, oldPosY, "top", 0))
			{
				statePlayer = STATE.NO_CHAO;
                pos.y = StaticObjects.walls[i].pos.y - size.y;
			}
			if (CollisionManager.CheckCollision(this, StaticObjects.walls[i], oldPosX, oldPosY, "bottom", 0))
                pos.y = StaticObjects.walls[i].pos.y + StaticObjects.walls[i].size.y;
			if (CollisionManager.CheckCollision(this, StaticObjects.walls[i], oldPosX, oldPosY, "left", 0))
                pos.x = StaticObjects.walls[i].pos.x - size.x;
			if (CollisionManager.CheckCollision(this, StaticObjects.walls[i], oldPosX, oldPosY, "right", 0))
                pos.x = StaticObjects.walls[i].pos.x + StaticObjects.walls[i].size.x;
		}
		
		oldPosX = pos.x;
		oldPosY = pos.y;
	}
	
	public void Draw(Graphics g)
	{
		super.Draw(g);
		leftArrow.Draw(g);
		rightArrow.Draw(g);
		jumpButton.Draw(g);
	}

}






